
package com.shop.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.portal.annotation.ApiLogin;
import com.shop.cloud.common.portal.entity.ThirdSession;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.mall.api.service.GoodsAppraisesService;
import com.shop.cloud.mall.api.service.OrderInfoService;
import com.shop.cloud.mall.api.service.UserInfoService;
import com.shop.cloud.mall.common.constant.MallReturnCode;
import com.shop.cloud.mall.common.entity.GoodsAppraises;
import com.shop.cloud.mall.common.entity.OrderInfo;
import com.shop.cloud.mall.common.entity.UserInfo;
import com.shop.cloud.mall.common.enums.OrderInfoEnum;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * 商品评价
 *
 * @author
 * @date 2019-09-23 15:51:01
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/goodsappraises")
@Tag(description = "goodsappraises", name = "商品评价Api")
public class GoodsAppraisesController {

	private final OrderInfoService orderInfoService;
    private final GoodsAppraisesService goodsAppraisesService;
	private final UserInfoService userInfoService;

	/**
    * 分页查询
    * @param page 分页对象
    * @param goodsAppraises 商品评价
    * @return
    */
	@Operation(summary = "分页查询")
    @GetMapping("/page")
    public R getGoodsAppraisesPage(Page page, GoodsAppraises goodsAppraises) {
        return R.ok(goodsAppraisesService.page2(page,goodsAppraises));
    }

    /**
    * 通过id查询商品评价
    * @param id
    * @return R
    */
	@Operation(summary = "通过id查询商品评价")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id){
        return R.ok(goodsAppraisesService.getById(id));
    }

    /**
    * 新增商品评价
    * @param listGoodsAppraises 商品评价
    * @return R
    */
	@Operation(summary = "新增商品评价")
    @PostMapping
	@ApiLogin
    public R save(@RequestBody List<GoodsAppraises> listGoodsAppraises){
		ThirdSession thirdSession = ThirdSessionHolder.getThirdSession();
		OrderInfo orderInfo = orderInfoService.getById(listGoodsAppraises.get(0).getOrderId());
		if(!OrderInfoEnum.STATUS_3.getValue().equals(orderInfo.getStatus())){
			return R.failed(MallReturnCode.ERR_70001.getCode(), MallReturnCode.ERR_70001.getMsg());
		}
		UserInfo userInfo = userInfoService.getById(thirdSession.getUserId());
		listGoodsAppraises.forEach(goodsAppraises -> {
			goodsAppraises.setShopId(orderInfo.getShopId());
			goodsAppraises.setUserId(userInfo.getId());
			goodsAppraises.setHeadimgUrl(userInfo.getHeadimgUrl());
			goodsAppraises.setNickName(userInfo.getNickName());
			if(StrUtil.isNotBlank(goodsAppraises.getParentId())){
				long count = goodsAppraisesService.count(Wrappers.<GoodsAppraises>lambdaQuery()
						.eq(GoodsAppraises::getParentId,goodsAppraises.getParentId()));
				if(count > 0){
					throw new RuntimeException("请不要重复追评");
				}
			}
		});
		goodsAppraisesService.doAppraises(listGoodsAppraises);
        return R.ok();
    }

    /**
    * 修改商品评价
    * @param goodsAppraises 商品评价
    * @return R
    */
	@Operation(summary = "修改商品评价")
    @PutMapping
	@ApiLogin
    public R updateById(@RequestBody GoodsAppraises goodsAppraises){
		goodsAppraises.setUserId(ThirdSessionHolder.getUserId());
        return R.ok(goodsAppraisesService.updateById(goodsAppraises));
    }

    /**
    * 通过id删除商品评价
    * @param id
    * @return R
    */
	@Operation(summary = "通过id删除商品评价")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id){
		return R.ok(goodsAppraisesService.removeById(id));
    }

	/**
	 * 分页查询我的评论
	 * @param page 分页对象
	 * @param goodsAppraises 商品评价
	 * @return
	 */
	@Operation(summary = "分页查询我的评论")
	@GetMapping("/page/user")
	@ApiLogin
	public R getGoodsAppraisesPageUser(Page page, GoodsAppraises goodsAppraises) {
		goodsAppraises.setUserId(ThirdSessionHolder.getUserId());
		return R.ok(goodsAppraisesService.page1(page,goodsAppraises));
	}
}
